package com.cheetah.dispatcher.factory;

import com.cheetah.dispatcher.Dispatcher;
import com.cheetah.dispatcher.impl.ProtobufDispatcher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @ClassName DispatcherFactory
 * @Author Administrator
 * @Date 2019/7/19
 * @Version V1.0
 * @Description: 分发器工厂，用以支持多种通讯协议请求的分发器
 **/

@Component
@Slf4j
public class DispatcherFactory implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public Dispatcher getDispatcher(String type){
        switch (type){
            case "protobuf" :
                return applicationContext.getBean(ProtobufDispatcher.class);
        }
        return applicationContext.getBean(ProtobufDispatcher.class);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
